package com.haipumi.redis;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RedisUtil {

	private SpringRedisClient springRedisClientImpl;

	//修改评论点赞数
	public Integer updateSupportNum(String rid,String uid){
		String mapKey="	"+rid;
		String numKey="SupportReplyNum"+rid;
		//取到评论的任务id
		Map<String, Boolean> map= springRedisClientImpl.getMap(mapKey);//评论该任务的集合
		if(map==null ){
			map=new LinkedHashMap<String, Boolean>();
		}
		String obj= (String) springRedisClientImpl.getKey(numKey);
		Integer num=0;
		if(obj==null || "".equals(obj)){//容错处理
			num=0;
		}else{
			num=Integer.parseInt(obj);
		}
		//判断用户是否已经点过赞? 点过=取消点赞
		if( map.containsKey(uid) ){//说明是取消点赞功能
			if(num>0){
				num--;
			}
			springRedisClientImpl.setKey(numKey, num.toString());
			map.remove(uid);
		}else{//说明是点赞功能
			//将点赞数++;
			num++;
			springRedisClientImpl.incr(numKey);
			map.put(uid, true);
		}
		springRedisClientImpl.setMap(mapKey,map);
		return num;
	}
	
	//获取评论点赞数量
	public String getSupportNum(String rid ){
		String numKey="SupportReplyNum"+rid;
		String num= (String) springRedisClientImpl.getKey(numKey);
		if(num==null || "".equals(num)){//容错处理
			num="0";
		}
		return num;
	}
	
	//记录List信息
	public void setListData(String key,String JsonParams){
		springRedisClientImpl.RPush(key, JsonParams);
	}
	
	//获取List消息
	public List<String> getListData(String key,long start,long end){
		return springRedisClientImpl.getList(key,start,end);
	}
	
	public void setSpringRedisClientImpl(SpringRedisClientImpl springRedisClientImpl) {
		this.springRedisClientImpl = springRedisClientImpl;
	}
}
